﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Web.Hosting;
using System.Xml;
using System.Xml.Serialization;

namespace yc.Site
{
	public class CommonConfigSerializerSectionHandler : IConfigurationSectionHandler
	{
		/// <summary>
		/// Creates an instance of the <see cref="AuthorizeConfig"/> class.
		/// </summary>
		/// <remarks>Uses XML Serialization to deserialize the XML in the Web.config file into an
		/// <see cref="AuthorizeConfig"/> instance.</remarks>
		/// <returns>An instance of the <see cref="AuthorizeConfig"/> class.</returns>
		public object Create(object parent, object configContext, XmlNode section)
		{
			// Create an instance of XmlSerializer based on the RewriterConfiguration type...
			XmlSerializer ser = new XmlSerializer(typeof(CommonConfiguration));
			// Return the Deserialized object from the Web.config XML
			return ser.Deserialize(new XmlNodeReader(section));
		}
	}

	[Serializable]
	[XmlRoot("commonConfig")]
	public class CommonConfiguration
	{
		[XmlElement("webSite")]
		public WebSiteConfig WebSite { set; get; }

		[XmlElement("pageSetting")]
		public PageSettingConfig PageSetting { set; get; }

		public CommonConfiguration()
		{
			WebSite = new WebSiteConfig();
			PageSetting = new PageSettingConfig();
		}

		/// <summary>
		/// 序列化存储
		/// </summary>
		/// <param name="filepath"></param>
		public void Save(string filepath = "~/commonConfig.config")
		{
			var file = HostingEnvironment.MapPath(filepath);
			var di = Directory.GetParent(file);
			if (di.Exists) di.Create();
			using (StreamWriter textWriter = new StreamWriter(file))
			{
				XmlSerializer xmlSerializer = new XmlSerializer(typeof(CommonConfiguration));
				xmlSerializer.Serialize(textWriter, this);
				textWriter.Flush();
			}
		}
	}

	/// <summary>
	/// 页面配置文件
	/// </summary>
	[Serializable]
	public class PageSettingConfig
	{
		/// <summary>
		/// 默认分页大小
		/// </summary>
		[XmlElement("defaultPageSize")]
		public int DefaultPageSize { set; get; }

		public PageSettingConfig()
		{
			DefaultPageSize = 20;//默认分页大小
		}
	}

	/// <summary>
	/// 网站设置
	/// </summary>
	[Serializable]
	public class WebSiteConfig
	{
		[XmlElement("xuaCompatible")]
		public string XUACompatible { set; get; }

		/// <summary>
		/// 网站标题
		/// </summary>
		[XmlElement("title")]
		public string Title { set; get; }

		/// <summary>
		/// 网站语言
		/// </summary>
		[XmlElement("language")]
		public string Language { set; get; }

		/// <summary>
		/// 
		/// </summary>
		[XmlElement("robots")]
		public string Robots { set; get; }

		/// <summary>
		/// 网站作者
		/// </summary>
		[XmlElement("author")]
		public string Author { set; get; }

		/// <summary>
		/// 网站版权
		/// </summary>
		[XmlElement("copyright")]
		public string CopyRight { set; get; }

		/// <summary>
		/// 
		/// </summary>
		[XmlElement("mssmartTagsPreventParsing")]
		public string MSSmartTagsPreventParsing { set; get; }

		/// <summary>
		/// 
		/// </summary>
		[XmlElement("imageToolBar")]
		public string ImageToolBar { set; get; }

		/// <summary>
		/// 网站描述
		/// </summary>
		[XmlElement("description")]
		public string Description { set; get; }

		/// <summary>
		/// 网站关键字
		/// </summary>
		[XmlElement("keywords")]
		public string KeyWords { set; get; }

		/// <summary>
		/// 静态变量过期时间：分钟
		/// </summary>
		[XmlElement("staticTimeOut")]
		public int StaticTimeOut { set; get; }

		/// <summary>
		/// DES加密密钥 8位字符串
		/// </summary>
		[XmlElement("deskey")]
		public string DesKey { set; get; }

		/// <summary>
		/// Session用户变量名
		/// </summary>
		[XmlElement("sessionUser")]
		public string SessionUser { get; set; }

		public WebSiteConfig()
		{
			XUACompatible = "IE=edge";
			Title = "尼奥工作室";
			Language = "";
			Robots = "all";
			Author = "";
			CopyRight = "© 2010-2012 www.lktec.cn All rights reserved.";
			MSSmartTagsPreventParsing = "yes";
			ImageToolBar = "false";
			Description = "";
			KeyWords = "";
			StaticTimeOut = 30;
			DesKey = "12345678";
			SessionUser = "_user_";
		}

	}
}
